let fun=require('./datbase');
function count(inputs){
    let arr=[];
    let obj={};
    let result=[];
    for(let item of inputs){
        if(/^[A-z]{4}[0-9]{6}[-]/.test(item)){
            let num=parseInt(item.replace(/[A-z]{4}[0-9]{6}[-]/,''));
            let barcode=item.replace(/[-][0-9]{1,5}$/,'');
            for(let i=1;i<=num;i++){
                arr.push(barcode);
            }
        }
        else{
            arr.push(item);
        }
        
    }
    for(let item of arr){
        if(!obj[item]){
            obj[item]=1;
        }
        else{
            obj[item]++;
        }
    }
    for(let i in obj){
        result.push({barcode:i,count:obj[i]});
    }
    return result;

}

function addInformation(afterCount){
    let allCommdity=fun.loadAllItems();
    for(let item1 of allCommdity){
        for(let item2 of afterCount){
            if(item1.barcode===item2.barcode){
                item2.name=item1.name;
                item2.unit=item1.unit;
                item2.price=item1.price;
            }
        }
    }
    return afterCount;
}
function promotionCommdity(commdity){
     function same(allPromotions,item1){
         for(let item2 of allPromotions){
             if(item2===item1.barcode){
                 return true;
             }
         }
     }
    let result={};
    let freeCommdity=[];
    let sum=0;
    let allPromotions=fun.loadPromotions()[0].barcodes;
    for(let item of commdity){
        if(same(allPromotions,item)){
            freeCommdity.push({
                barcode:item.barcode, 
                count:Math.floor(item.count/3),
                name:item.name,
                unit:item.unit,
                price:item.price,
                subtotal:item.price*(Math.floor(item.count/3))
                                })
            item.subtotal=(item.count-Math.floor(item.count/3))*item.price;
            
            }
            else{
                item.subtotal=item.count*item.price;
            }

        
    }
        result={promotion:freeCommdity,cart:commdity};
    return result;
}
function printText(commdity,promotionCommdity){
    let result=`***<没钱赚商店>购物清单***\n`;
    let sum=0;
    let saveMoney=0;
    for(let item of commdity){
        result+=`名称：${item.name}，数量：${item.count}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${item.subtotal.toFixed(2)}(元)\n`
        sum+=item.subtotal;  
    }
    result+=`----------------------\n`;
    if(promotionCommdity.promotion.length!==0){
        result+=`挥泪赠送商品：\n`;
        for(let item of promotionCommdity.promotion){
            result+=`名称：${item.name}，数量：${item.count}${item.unit}\n`;
            saveMoney+=item.subtotal;
        }
        result+=`----------------------\n总计：${sum.toFixed(2)}(元)\n节省：${saveMoney.toFixed(2)}(元)\n**********************`;
    }
    else{
        result+=`总计：${sum.toFixed(2)}(元)\n**********************`;
    }
    return result;
}
function printInventory(inputs){
    let afterCount=count(inputs);
    let afterAdd=addInformation(afterCount);
    let promotionsInformation=promotionCommdity(afterAdd);
    let result=printText(afterAdd,promotionsInformation);
    return result;
}
module.exports = printInventory; 
